using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;

namespace BTV.Base
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1705:LongAcronymsShouldBePascalCased")]
    public class BTVChannel
    {
        private string _displayName;
        /// <summary>
        /// Display Name, used for bleb channels
        /// </summary>
        public string DisplayName
        {
            get { return _displayName; }
            set { _displayName = value; }
        }
        private string _name;

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
        private string _id;

        public string Id
        {
            get { return _id; }
            set { _id = value; }
        }
        private Dictionary<string, BTVProgram> _programs = new Dictionary<string, BTVProgram>();

        public Dictionary<string, BTVProgram> Programs
        {
            get { return _programs; }
            //set { _programs = value; }
        }
        private Dictionary<string, List<BTVSchedule>> _schedules = new Dictionary<string, List<BTVSchedule>>();

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")]
        public Dictionary<string, List<BTVSchedule>> Schedules
        {
            get { return _schedules; }
            //set { _schedules = value; }
        }
        private bool _isTVAny = true;

        public bool IsTVAny
        {
            get { return _isTVAny; }
            set { _isTVAny = value; }
        }

        public BTVProgram GetProgram(string crid)
        {
            BTVProgram p;
            if (_programs.TryGetValue(crid, out p))
            {
                return p;
            }
            return null;
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1804:RemoveUnusedLocals", MessageId = "argEx")]
        public void AddProgram(BTVProgram p)
        {
            try
            {
                _programs.Add(p.PID, p);
            }
            catch { 
                //ignore error
                //Console.WriteLine("Exception in TVAny_Channel.addProgram adding prog Id "+p.pid); 
            }
        }

        public void AddSchedule(BTVSchedule s)
        {
            List<BTVSchedule> g = new List<BTVSchedule>();
            if (_schedules.TryGetValue(s.Pid, out g))
            {
                g.Add(s);
            }
            else
            {
                g = new List<BTVSchedule>();
                g.Add(s);
                _schedules.Add(s.Pid, g);
            }
        }

        public BTVProgramSchedulePairCollection GetProgramsInHour(int hour)
        {
            BTVProgramSchedulePairCollection ret = new BTVProgramSchedulePairCollection();
            foreach (KeyValuePair<string, List<BTVSchedule>> kvp in _schedules)
            {
                foreach ( BTVSchedule s in kvp.Value )
                {
                    if ( s.StartTime.Hour.Equals(hour) )
                    {
                        ret.Add(new BTVProgramSchedulePair(this.GetProgram(s.Pid), s));
                        //ret.Add(s, this.GetProgram(s.Pid));
                    }
                    /*else
                    {
                        if ( s.EndTime.Hour.Equals(hour) )
                        {
                            ret.Add(s, this.GetProgram(s.Pid));
                        }
                    }*/
                }
            }
            return ret;
        }

        public void Reset()
        {
            _programs.Clear();
            _schedules.Clear();
        }

        public override string ToString()
        {
            if (string.IsNullOrEmpty(DisplayName))
                return Name;
            else
                return DisplayName;
        }
    }
}
